package main

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"tio-proxy-agent/client"
	"tio-proxy-agent/core"
)

func main() {
	var key = os.Args[1]
	var url = "https://www.jlearn.cn/proxy/api/open/proxy/client/tunnel/" + key
	resp, err := http.Get(url)
	if err != nil {
		return
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			return
		}
	}(resp.Body)
	bs, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	r := &client.Result{}
	_ = json.Unmarshal(bs, r)
	m := r.Data.(map[string]interface{})

	client.TunnelInfo = &client.Tunnel{
		Token:      key,
		Domain:     m["domain"].(string),
		TargetHost: m["targetHost"].(string),
		TargetPort: int(m["targetPort"].(float64)),
		Protocol:   m["protocol"].(string),
	}

	config := new(client.GioClientConfig)
	reconnectConfig := &core.ReconnectConfig{
		RetryCount: 5,
		Interval:   5000,
	}
	gioClient := client.NewGioClient(config, reconnectConfig)
	gioClient.Connect("124.70.200.206", 10011)

}
